'''
Created on Sep 29, 2009

@author: wangjo
'''

from google.appengine.api import users
from google.appengine.ext import db

class orzzing(db.Model):
    title = db.StringProperty(required = True)
    author = db.UserProperty(required = True)
    source = db.LinkProperty()
    type = db.StringProperty(choices=['photo','youtube','youku','tudou','misc'], default='misc')
    content = db.TextProperty()
    archived = db.BooleanProperty(default=False)
    reviewed = db.BooleanProperty(default=False)
    count_view = db.IntegerProperty(default = 0)
    count_like = db.IntegerProperty(default = 0)
    count_dislike = db.IntegerProperty(default = 0)
    created = db.DateTimeProperty(auto_now_add=True)
    updated = db.DateTimeProperty(auto_now=True) 
    
    DEFAULT_PAGE_COUNT = 10
    @staticmethod
    def get_current_user_lists():
        return orzzing.get_user_lists(users.get_current_user())
    
    @staticmethod
    def get_user_lists(user):
        if not user: return []
        return db.Query(orzzing).filter('author =', user) 
     
    @staticmethod
    def get_latest_orzzing(count = DEFAULT_PAGE_COUNT):
        if int(count) < 0: 
            count = orzzing.DEFAULT_PAGE_COUNT
        return db.Query(orzzing).filter('reviewed =', True).order('-created').fetch(int(count)) 
    
    def get_comment_count(self):
        oc = db.Query(orzzingComments).filter('orzzing =', self)
        return oc.count()   
    
    def get_tags(self):
        tags = db.Query(orzzingTag).filter('orzzing =', self)
        return [t.tag for t in tags]
    
    def update_poke_status(self, likeit = True):
        if likeit:
            self.count_like += 1
        else:
            self.count_dislike += 1
        self.put()
    
    def is_favored_by_current_user(self):
        return db.Query(favorite).filter('orzzing =', self).filter('user =', users.get_current_user()).get()
           
class comment(db.Model):
    author = db.UserProperty()
    ip = db.StringProperty()
    title = db.StringProperty()
    content = db.TextProperty()
    reply = db.SelfReferenceProperty('parent', 'children')
    created = db.DateTimeProperty(auto_now_add=True)
    updated = db.DateTimeProperty(auto_now=True) 
    
    @staticmethod
    def get_orzzing_comments(orzzing):
        if not orzzing: return []
        commentlist = db.Query(orzzingComments).filter('orzzing =', orzzing)
        return [c.comment for c in commentlist]
    
class orzzingComments(db.Model):
    orzzing = db.Reference(orzzing, required = True)
    comment = db.ReferenceProperty(comment, required = True)
    
class tag(db.Model):
    name = db.StringProperty(required = True)
    count = db.IntegerProperty(default = 0)
    is_category = db.BooleanProperty(default = False)
    created = db.DateTimeProperty(auto_now_add=True)
    updated = db.DateTimeProperty(auto_now=True) 
    
class orzzingTag(db.Model):
    orzzing = db.ReferenceProperty(orzzing, required = True)
    tag = db.ReferenceProperty(tag, required = True)
    author = db.UserProperty(required = True)

class favorite(db.Model):
    user = db.UserProperty(required = True)
    orzzing = db.ReferenceProperty(required = True)
    created = db.DateTimeProperty(auto_now_add=True)
    
    @staticmethod
    def get_orzzing_favor_count(orzzing):
        if not orzzing: return 0;
        return db.Query(Favorite).filter('orzzing =', orzzing).count()
    
        
class poke(db.Model):
    user = db.UserProperty()
    ip = db.StringProperty()
    orzzing = db.ReferenceProperty(orzzing, required = True)
    likeit = db.BooleanProperty(required = True)
    created = db.DateTimeProperty(auto_now_add=True)
    
    @staticmethod
    def is_currentuser_voted(orzzing):
        if not orzzing: return False
        if not users.get_current_user(): return False
        return db.Query(poke).filter('user = ', users.get_current_user()).filter('orzzing =', orzzing).get()
    
    @staticmethod
    def get_orzzing_count(orzzing, likeit = True):
        if not orzzing: return 0
        return db.Query(poke).filter('orzzing =', orzzing).filter('likeit =', likeit).count()
    
    
